﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MS.Core.Encryption;
using MS.Core.Constants.Objects;
using LHS.Core.TableInfo;
using System.Collections.Specialized;
using System.Collections;

namespace LHS.Core.Authority
{
    public class CommonAuthority : MS.Core.Base.Data.DBBaseData
    {
        #region Variable
        private Encryptor myEncrypt;
        private Decryptor myDecrypt;
        #endregion

        #region Properties
        private Encryptor _myEncrypt
        {
            get
            {
                if (myEncrypt == null)
                {
                    myEncrypt = new Encryptor(EncryptionAlgorithm.TripleDes);
                }
                return myEncrypt;
            }
        }

        private Decryptor _myDecrypt
        {
            get
            {
                if (myDecrypt == null)
                {
                    myDecrypt = new Decryptor(EncryptionAlgorithm.TripleDes);
                }
                return myDecrypt;
            }
        }
        #endregion

        /// <summary>
        /// login vào hệ thống
        /// </summary>
        /// <param name="strUserName"></param>
        /// <param name="strPassword"></param>
        /// <returns></returns>
        public DataTable LoginSystem(string strUserName, string strPassword)
        {
            DataTable dt = InfoUsers.GetStructure();
            try
            {
                object iCount = base.Count(InfoUsers.USRISN, InfoUsers.TABLENAME);
                if (iCount is int)
                {
                    if ((int)iCount == 0)
                    {
                        // insert row default vao he thong
                        //user = sa
                        //pwd = encrypt cua sa
                        strUserName = "sa";
                        strPassword = _myEncrypt.Encrypt(strUserName, MS.Core.Constants.GlobalContanst.My_Keys, MS.Core.Constants.GlobalContanst.My_IV);
                        DataRow dr = dt.NewRow();
                        dr[InfoUsers.USRNAME] = strUserName;
                        dr[InfoUsers.USRPASSWORD] = strPassword;
                        dr[InfoUsers.ROLISN] = 0; //20091122
                        dt.Rows.Add(dr);
                        try
                        {
                            base.InsertRow(dr, new string[] { InfoUsers.USRISN });
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    else
                    {
                        strPassword = _myEncrypt.Encrypt(strPassword, MS.Core.Constants.GlobalContanst.My_Keys, MS.Core.Constants.GlobalContanst.My_IV);
                    }
                    List<ExpressionObject> objParam = new List<ExpressionObject>();
                    objParam.Add(new ExpressionObject(InfoUsers.USRNAME, strUserName));
                    objParam.Add(new ExpressionObject(InfoUsers.USRPASSWORD, strPassword));
                    dt = base.GetData(InfoUsers.TABLENAME, objParam);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// thay đổi password 
        /// </summary>
        /// <param name="strUserName"></param>
        /// <param name="strPassword"></param>
        public void ChangePassword(string strUserName, string strPassword)
        {
            try
            {
                strPassword = _myEncrypt.Encrypt(strPassword, MS.Core.Constants.GlobalContanst.My_Keys, MS.Core.Constants.GlobalContanst.My_IV);
                ListDictionary updateField = new ListDictionary();
                updateField.Add(InfoUsers.USRPASSWORD, strPassword);

                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject(InfoUsers.USRNAME, strUserName));
                base.UpdateRow(InfoUsers.TABLENAME, updateField, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// thêm một user vào trong hệ thống
        /// </summary>
        /// <param name="dr"></param>
        public void InsertUser(DataRow dr)
        {
            try
            {
                dr[InfoUsers.USRPASSWORD] = _myEncrypt.Encrypt(dr[InfoUsers.USRPASSWORD].ToString(), MS.Core.Constants.GlobalContanst.My_Keys, MS.Core.Constants.GlobalContanst.My_IV);

                base.InsertRow(dr, new string[] { InfoUsers.USRISN });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// back up database tự động
        /// </summary>
        /// <param name="hashBackUp"></param>
        public void BackUpDatabaseAuto(Hashtable hashBackUp)
        {
            try
            {
                ListDictionary param = new ListDictionary();
                param.Add(InfoBackup.FREQTYPE, hashBackUp[InfoBackup.FREQTYPE]);
                param.Add(InfoBackup.FREQINTERVAL, hashBackUp[InfoBackup.FREQINTERVAL]);
                param.Add(InfoBackup.FREQSUBDAYTYPE, hashBackUp[InfoBackup.FREQSUBDAYTYPE]);
                param.Add(InfoBackup.FREQSUBDAYINTERVAL, hashBackUp[InfoBackup.FREQSUBDAYINTERVAL]);
                param.Add(InfoBackup.ACTIVESTARTTIME, hashBackUp[InfoBackup.ACTIVESTARTTIME]);
                param.Add(InfoBackup.ACTIVEENDTIME, hashBackUp[InfoBackup.ACTIVEENDTIME]);
                param.Add(InfoBackup.FREQRECURRENCEFACTOR, hashBackUp[InfoBackup.FREQRECURRENCEFACTOR]);
                param.Add(InfoBackup.FILEPATH, hashBackUp[InfoBackup.FILEPATH]);

                base.LoadFromSqlScalar("sp_BackUpAuto", param, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// backup database bang tay
        /// </summary>
        /// <param name="hashBackUp"></param>
        public void BackUpDatabaseManual(Hashtable hashBackUp)
        {
            try
            {
                ListDictionary param = new ListDictionary();
                param.Add(InfoBackup.FILENAME, hashBackUp[InfoBackup.FILENAME]);
                param.Add(InfoBackup.FILEPATH, hashBackUp[InfoBackup.FILEPATH]);

                base.LoadFromSqlScalar("sp_BackUpManual", param, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lay thong tin backup
        /// </summary>
        /// <param name="hashBackUp"></param>
        public DataTable GetBackUpDT()
        {
            try
            {
               return base.LoadFromSql("SELECT " + InfoBackup.FREQTYPE + ", " + InfoBackup.FREQINTERVAL + ", " + InfoBackup.FREQSUBDAYTYPE + "," + InfoBackup.FREQSUBDAYINTERVAL + "," + InfoBackup.ACTIVESTARTTIME + "," + InfoBackup.ACTIVEENDTIME + "," + InfoBackup.FREQRECURRENCEFACTOR + " FROM  " + InfoBackup.TABLENAME + " Where " + InfoBackup.BCKISN + " = (Select max(" + InfoBackup.BCKISN + ") From " +InfoBackup.TABLENAME+")").Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
